<template>
  <el-card class="latest-data-card">
    <template #header>
      <div class="card-header">
        <h3>最新健康数据</h3>
      </div>
    </template>
    
    <div v-if="data.length === 0" class="no-data">
      <p>暂无健康数据</p>
      <el-button type="primary" @click="$router.push('/health-data')">
        添加健康数据
      </el-button>
    </div>
    
    <div v-else>
      <el-table :data="data" style="width: 100%">
        <el-table-column prop="data_type" label="数据类型" />
        <el-table-column label="数值">
          <template #default="scope">
            {{ scope.row.value }} {{ scope.row.unit }}
          </template>
        </el-table-column>
        <el-table-column label="记录时间">
          <template #default="scope">
            {{ formatDate(scope.row.created_at) }}
          </template>
        </el-table-column>
      </el-table>
      
      <div class="view-more">
        <el-button type="text" @click="$router.push('/health-data')">
          查看更多 <el-icon><ArrowRight /></el-icon>
        </el-button>
      </div>
    </div>
  </el-card>
</template>

<script>
import { ArrowRight } from '@element-plus/icons-vue'

export default {
  name: 'LatestHealthData',
  components: {
    ArrowRight
  },
  props: {
    data: {
      type: Array,
      required: true
    }
  },
  setup() {
    const formatDate = (dateString) => {
      const date = new Date(dateString);
      return date.toLocaleString('zh-CN', {
        year: 'numeric',
        month: '2-digit',
        day: '2-digit',
        hour: '2-digit',
        minute: '2-digit'
      });
    };
    
    return {
      formatDate
    };
  }
};
</script>

<style scoped>
.latest-data-card {
  height: 100%;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.card-header h3 {
  margin: 0;
  color: #409EFF;
}

.no-data {
  text-align: center;
  padding: 20px 0;
}

.no-data p {
  margin-bottom: 20px;
  color: #909399;
}

.view-more {
  text-align: right;
  margin-top: 15px;
}
</style> 